import {wxLogin,getUser} from '@/api/global'
import {setToken} from '@/utils/request'
let jsCode = '';


//token过期处理
export function tokenExpire() {
  uni.login({
    success:function(res){
      if(res.code){
        // console.log(res.code)
        jsCode = res.code;
        login();
      }else {
        console.log("登录失败");
      }
    }
  })
}
function login() {
  wxLogin({code:jsCode})
    .then(response => {
      var _data = response[1].data;
      if(_data.status == 200) {
        uni.setStorageSync("token",_data.data.token);
        // setToken(_data.data.token);
        setToken('123456');
        userInfo();
      }
    })
    .catch(error => {
      console.log(error);
    })
}
function userInfo() {
  //通过后端获取用户信息
  getUser({}).then(response => {
    // console.log(response)
    var _data = response[1].data;
    if(_data.status == 200) {
      var user = _data.data;
      //缓存个人信息
      uni.setStorageSync('user',user);
    }
  })
}